package com.oneonefive.trainticketsystem.controller;

import cn.hutool.core.bean.BeanUtil;
import com.oneonefive.trainticketsystem.DTO.UserDTO;
import com.oneonefive.trainticketsystem.pojo.User;
import com.oneonefive.trainticketsystem.service.UserService;
import jakarta.servlet.http.HttpSession;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@CrossOrigin
@RestController
public class UserController {
    final
    UserService userService;

    public UserController(UserService userService) {
        this.userService = userService;
    }

    @RequestMapping(value = "/loginCheck", method = RequestMethod.POST)
    public UserDTO loginCheck(@RequestBody User user, HttpSession session) {
        UserDTO userDTO = userService.loginCheck(user, session);
        return userDTO;
    }

    @RequestMapping("/checkUser")
    public int checkUser(@RequestBody User user) {
        User byId = userService.getById(user.getUserId());
        if (byId == null) {
            userService.save(user);
            return 0;
        } else return 1;
    }

    @RequestMapping("/selectUserById")
    public UserDTO selectUserById(Integer id) {
        User user = userService.getById(id);
        UserDTO userDTO = BeanUtil.copyProperties(user, UserDTO.class);
        return userDTO;
    }

    @RequestMapping("/saveOrUpdateUser")
    public boolean saveOrUpdateUser(User user) {
        return userService.saveOrUpdate(user);
    }

    @RequestMapping("/selectAllUser")
    public List<User> selectAllTrain(Integer userId, String userName, Integer page) {
        return userService.getAllUser(userId, userName, (page - 1) * 10);
    }

    @RequestMapping("/getUserRowCount")
    public int getRowCount(Integer userId, String userName) {
        return userService.getRowCount(userId, userName);
    }

    @RequestMapping("/deleteUserById")
    public boolean deleteUserById(Integer userId) {
        return userService.removeById(userId);
    }
}
